/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pos.services.impl;

import com.pos.domain.Role;
import com.pos.domain.User;
import com.pos.ejb.UserEJB;
import com.pos.services.UserService;
import java.util.List;
import javax.ejb.EJB;
import javax.inject.Named;

/**
 *
 * @author suraj
 */
@Named
public class UserServiceImpl implements UserService {

    @EJB
    private UserEJB userEjb;

    public List<User> findAll() {
        return this.userEjb.findAll();
    }

    public User findByLodinId(String loginId) {
        return this.userEjb.findById(loginId);
    }

    private void createUser(User user, String[] rolesName) {
        Role role = null;
        for (int i = 0; i < rolesName.length; i++) {
            role = new Role(rolesName[i]);
            user.addRole(role);
            role = null;
        }
        if (user.getLoginId().equals("") || findByLodinId(user.getLoginId()) == null) {
            this.userEjb.create(user);
        } else {
            this.userEjb.edit(user);
        }
    }

    public void addEmployee(User user) {
        this.createUser(user, Role.EMPLOYEE);
    }

    public void addManager(User user) {
        this.createUser(user, Role.MANAGER);
    }

    public void addAdministrator(User user) {
        this.createUser(user, Role.ADMINISTRATOR);
    }
}
